﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using System.ServiceModel;
using System.ServiceModel.Activation;
using TVGridContracts;

namespace TVGridServices
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple,
        IncludeExceptionDetailInFaults = true)]
    public class TVGridService:ITVGridService
    {
        private DBNegotiator dbNegotiator = new DBNegotiator();
        public int RegisterNewUser(string UserName, string Password)
        {
            try
            {
                User user = new User();
                user.UserName = UserName;
                user.Password = Password;

                int id = dbNegotiator.RegisterUser(user);
                return id;
            }
            catch(Exception exc)
            {
                throw new InvalidOperationException("Cant register to server, the server is down or there is no connection to internet", exc);
            }
           
        }

        public int GetUserID(string UserName, string Password)
        {
            try
            {
                int id = dbNegotiator.GetUserID(UserName, Password);
                return id;
            }
            catch
            {
                throw new Exception("Error geting user id");
            }
        }

        public List<TVStation> GetUserStations(int UserID)
        {
            List<Station> stations = dbNegotiator.GetUserStations(UserID);
            List<TVStation> StationsList = new List<TVStation>();

            if (stations != null)
            {
                foreach (var item in stations)
                {
                    TVStation station = new TVStation();
                    station.Address = new Uri(item.URL);
                    station.StationID = item.ID;
                    station.StationName = item.Name;
                    StationsList.Add(station);
                }
            }

            return StationsList;
        }

        public void SaveUserStations(int UserID, int[] Stations)
        {
            try
            {
                if (Stations.Length > 0)
                {
                    List<UserSetting> settings = new List<UserSetting>();
                    for (int i = 0; i < Stations.Length; i++)
                    {
                        settings.Add(new UserSetting() { UserID = UserID, TVStation = Stations[i] });
                    }
                    dbNegotiator.SaveUserStations(settings);
                }
            }
            catch
            {
                throw new Exception("Saving settings to server failed");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<TVStation> GetStations()
        {
            List<Station> stations = dbNegotiator.GetStations();
            if (stations != null)
            {
                List<TVStation> tvStations = new List<TVStation>();
                foreach (var item in stations)
                {
                    tvStations.Add(new TVStation() { StationID = item.ID, StationName = item.Name, Address = new Uri(item.URL)  });
                }
                return tvStations;
            }
            return null;
        }

        public string GetStationURL(int StationID)
        {
            Station station = dbNegotiator.GetStationByID(StationID);
            if (station != null)
                return station.URL;
            else
                return string.Empty;
        }

        public void AddStation(string Name, string URL)
        {
            if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(URL))
            {
                try
                {
                    dbNegotiator.AddStation(new Station() { Name = Name, URL = URL });
                }
                catch
                {
                    throw new Exception("Failed to add station to server");
                }
            }
        }

        public void RemoveStation(Station station)
        {
            if (station != null)
                dbNegotiator.RemoveStation(station);
        }
    }
}
